package cn.edu.scau.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.edu.scau.bean.ResultBean;
import cn.edu.scau.entity.NursingContent;
import cn.edu.scau.service.NursingContentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;


@Api(tags = "护理信息控制器")
@RestController
@RequestMapping("nursingcontent")
public class NursingContentController {
	@Autowired
	private NursingContentService nursingContentService;

	/**
	  * 查询所有护理内容信息 
	 * @return
	 */
	@ApiOperation(value = "返回所有护理内容信息")
	@GetMapping(value = "/list")
	public ResultBean list(@RequestParam(value = "pageNum",defaultValue = "1")int pageNum,
			@RequestParam(value = "pageSize",defaultValue = "5")int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		List<NursingContent> nursingContents=nursingContentService.selectAll();
		PageInfo<NursingContent> pageInfo = new PageInfo<NursingContent>(nursingContents,pageSize);
		ResultBean resultBean =new ResultBean(200,true,"查询所有护理内容信息成功~~~",pageInfo);
		return resultBean;
	}
	

	/**
	  *  根据护理内容项目名称模糊查询
	 */
	@ApiOperation(value = "根据护理内容项目名称模糊查询")
	@ApiImplicitParams(value = {
			@ApiImplicitParam(paramType = "query",name="nursingcontentprojectname",value = "护理内容项目名称",dataType = "String")
	})
	@GetMapping(value = "/find")
	public ResultBean find(String nursingcontentprojectname,
			@RequestParam(value = "pageNum",defaultValue = "1")int pageNum,
			@RequestParam(value = "pageSize",defaultValue = "5")int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		List<NursingContent> nursingContents=nursingContentService.findByNursingcontentprojectname(nursingcontentprojectname);
		PageInfo<NursingContent> pageInfo = new PageInfo<NursingContent>(nursingContents);
		ResultBean r =new ResultBean(200,true,"模糊查询成功~~~",pageInfo);
		return r;
	}
	
	
	/**
	 * 新增护理内容
	 * @param nursingContent
	 * @param bindingResult
	 * @return
	 */
	@PostMapping(value = "/insert")
	public ResultBean insert(@Validated NursingContent nursingContent,BindingResult bindingResult)  {
		ResultBean resultBean =null;
		if(bindingResult.hasErrors()) {
			//方法一：将无法通过数据校验的信息，合并成一个字符串，返回给前端
			StringBuffer msg = new StringBuffer();
			for(FieldError f: bindingResult.getFieldErrors()) {
				msg.append(f.getField()+"："+f.getDefaultMessage()+"\n");
			}
			resultBean=new ResultBean(5005,false,msg.toString(),null);
			//方法二：将所有无法通过数据校验的信息集合，合并成一个字符串，返回给前端
//			resultBean=new ResultBean(5005,false,"实体对象赋值异常",null);
			return resultBean;
		}
		boolean flag = nursingContentService.insert(nursingContent);
		if(flag) {
			resultBean = new ResultBean(200,true,"新增护理内容信息成功~~~\n",null);
		}else {
			resultBean = new ResultBean(5005,false,"新增护理内容信息失败",null);
		}
		return resultBean;
	}

	/**
	  * 根据编号查询护理内容信息
	 * @param nursingContentid
	 * @return
	 */
	@ApiOperation(value = "根据护理内容编号查询信息")
	@ApiImplicitParam(paramType = "path",name = "nursingcontentid",value = "编号",required = true,dataType = "int")
	@GetMapping(value = "/{nursingcontentid}")
	public ResultBean detail(@PathVariable("nursingcontentid")Integer nursingcontentid) {
		NursingContent nursingContent = nursingContentService.selectById(nursingcontentid);
		ResultBean resultBean=new ResultBean(200,true,"查询成功~~~",nursingContent);
		return resultBean;
	}
	

	/**
	  * 修改护理内容信息
	 * @param nursingContentid
	 * @param nursingContent
	 * @param bindingResult
	 * @return
	 */
	@PutMapping(value = "/update/{nursingcontentid}")
	public ResultBean update(@PathVariable("nursingcontentid")Integer nursingcontentid,@Validated NursingContent nursingContent,BindingResult bindingResult) {
		ResultBean resultBean =null;
		if(bindingResult.hasErrors()) {
			//方法一：将无法通过数据校验的信息，合并成一个字符串，返回给前端
			StringBuffer msg = new StringBuffer();
			for(FieldError f: bindingResult.getFieldErrors()) {
				msg.append(f.getField()+"："+f.getDefaultMessage()+"\n");
			}
			resultBean=new ResultBean(5005,false,msg.toString(),null);
			//方法二：将所有无法通过数据校验的信息集合，合并成一个字符串，返回给前端
//			resultBean=new ResultBean(5005,false,"实体对象赋值异常",null);
			return resultBean;
		}

		boolean flag = nursingContentService.update(nursingContent);
		if(flag) {
			resultBean = new ResultBean(200,true,"修改护理内容成功~~~\n修改护理内容编号为"+nursingcontentid.intValue(),null);
		}else {
			resultBean = new ResultBean(5005,false,"修改护理内容失败",null);
		}
		return resultBean;
	}
	

	/**
	  * 根据编号删除护理内容信息
	 * @param nursingContentid
	 * @return
	 */
	@DeleteMapping(value = "/delete/{nursingcontentid}")
	public ResultBean delete(@PathVariable("nursingcontentid")Integer nursingcontentid) {
		boolean flag = nursingContentService.delete(nursingcontentid);
		ResultBean resultBean =null;
		if(flag) {
			resultBean = new ResultBean(200,true,"删除护理内容成功~~~\n删除护理内容编号为"+nursingcontentid,null);
		}else {
			resultBean = new ResultBean(5005,false,"删除护理内容失败",null);
		}
		return resultBean;
	}
}
